<?php

function ns_lab_populate_ircam()
{
	
	//create new document object
	$dom_object = new DOMDocument();
	
	//load xml file
	$dom_object->load(get_template_directory_uri()."/lab/dict.xml");
	$xml_raw = $dom_object->getElementsByTagName("word_entry");
	
	if ( empty($wp_error) )
		$wp_error = new WP_Error();
	
	$i = 0;
	
	echo '<ul>';
		
	foreach( $xml_raw as $value )
	{	
		$tmz_words = $value->getElementsByTagName("index_tiffinagh1");
		$tmz_single  = $tmz_words->item(0)->nodeValue;
		
		$translations_node = $value->getElementsByTagName("index_tiffinagh1_F");
		$translations_array  = $translations_node->item(0)->nodeValue;
		
		// does the word have many synonyms?
		$syno_check = strpos($tmz_single, ',');
		if ($syno_check !== false) 
		{
			$entries = explode(",", $tmz_single);
			
			// loop through synonyms
			foreach( $entries as $entry )
			{
				$inserted = ns_lab_prepare_insert( $entry, $translations_array, $wp_error );
				if ( !is_wp_error($inserted) ){
					//echo '<li>inserted : '.$entry.' and translated : '.$translations_array.'</li>';
				} else
				{	
					//echo '<li>not inserted : '.$entry.' with translation : '.$translations_array.'</li>';
					
					$code = $wp_error->get_error_codes();
					if( $code[0] != 'word_is_duplicate' )
						break 2;
				}
			}
		} 
		else 
		{
			$inserted = ns_lab_prepare_insert( $tmz_single, $translations_array, $wp_error );
			if ( !is_wp_error($inserted) ){
				//echo '<li>inserted : '.$tmz_single.' and gave her ID: '.$inserted.'</li>';
			} else
			{
				$code = $wp_error->get_error_codes();
				if( $code[0] != 'word_is_duplicate' )
					break 2;
			}
		}
		
		$i++;
	}
	if ( $wp_error->get_error_code() ) {
		$errors = '';
		$messages = '';
		foreach ( $wp_error->get_error_codes() as $code ) {
			$severity = $wp_error->get_error_data($code);
			foreach ( $wp_error->get_error_messages($code) as $error ) {
				if ( 'message' == $severity )
					$messages .= '	' . $error . "<br />\n";
				else
					$errors .= '	' . $error . "<br />\n";
			}
		}
		if ( !empty($errors) )
			echo '<li>' . apply_filters('add_word_errors', $errors) . "</li>";
		if ( !empty($messages) )
			echo '<li>' . apply_filters('add_word_messages', $messages) . "</li>";
	}
	echo '</ul>';
	
}
add_action( 'ns_lab_populate_ircam', 'ns_lab_populate_ircam' ); 

/**
 * Get the beggining of the word
 *
 * @since Twenty Eleven 1.0
 */
function ns_lab_prepare_insert( $entry, $translations_array, $wp_error = '' ) 
{	
	// construct post array
	$post = array();
	
	$translation = explode("/", $translations_array);
	
	// fill it
	$post['word'] = $entry;
	$post['content'] = $translation[0].', '.$translation[1];
	
	$post['translation_language'][1] = 'fr';
	$post['translation'][1] = $translation[0];
	
	$post['translation_language'][2] = 'en';
	$post['translation'][2] = $translation[1];
	
	$post['meta_word_region'] = 'IRCAM';
	
	// post it
	return ns_add_word( $post, $wp_error );
}